package com.weight.data.model;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;

import android.content.Intent;
import android.database.Cursor;

import com.weight.AppWeight;
import com.weight.R;
import com.weight.data.DataBaseHelper;
import com.weight.util.Constants;
import com.weight.util.Utilities;

public class Registry {
	
	private static Registry _instance;
	
	public static Registry getInstance(){
		if(_instance==null){
			_instance = new Registry();
		}
		return _instance;
	}
    /**
     * Saves one log to the Registry
     * @param calendar Date of the Log
     * @param weight Weight of the Log
     * @param kilo is it kilograms or pounds
     */
	public void saveLogInRegistry(Calendar calendar, float weight, boolean kilo){
		/*
		 * Convert to the right measure
		 */
		if(!kilo) {
			weight = Utilities.poundsToKilo(weight);
		}
		Log log = new Log(calendar,weight);
		DataBaseHelper.getInstance().addWeightLog(log);
	}
    
    /**
     * updates one log to the Registry
     * @param calendar Date of the Log
     * @param weight Weight of the Log
     * @param kilo is it kilograms or pounds
     */
	public boolean updateLogRegistry(int position, String dateStr, String weightStr, boolean kilo) {
		boolean result = false; 
		try {		
		    Log log = getLog(position,kilo);
			float weight = Float.parseFloat(weightStr);
			/*
			 * Convert to the right measure
			 */
			if(!kilo) {
				weight = Utilities.poundsToKilo(weight);
			}
			Date date = DateFormat.getDateInstance().parse(dateStr);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			log.setDate(calendar);
			log.setWeight(weight);
			DataBaseHelper.getInstance().updateWeightLog(position,log);
			result = true;
		} catch(Exception e){
			e.printStackTrace();
		}
		return result;
	}
	
	
	public Log getLog(int position, boolean kilo){
		Log log = null;
		try{
		Cursor cursor = DataBaseHelper.getInstance().getLog(position);
			if(cursor!=null && cursor.moveToFirst()) {
		      	float weigth = cursor.getFloat(cursor.getColumnIndex(Constants.WEIGHT_TABLE_WEIGHT_COLUMN));
		      	/**
		      	 * Convert to the proper weight measure
		      	 */
		      	if(!kilo) {
		      		weigth = Utilities.kiloToPounds(weigth);
		      	}
		      	String dateString = cursor.getString(cursor.getColumnIndex(Constants.WEIGHT_TABLE_DATE_COLUMN));
		      	Date date = DateFormat.getDateInstance().parse(dateString);
		      	Calendar calendar = Calendar.getInstance();
		      	calendar.setTime(date);
		      	log = new Log(calendar,weigth);
			}
		} catch(Exception e){
			e.printStackTrace();
		}
		return log;
	}
    public String createHTMLReport( Cursor cursor ) {
        String result = "<html> <body><table border=\"1\"> <tr> <th> <h2>" + AppWeight.getInstance().getString(R.string.reportdatecolumn) +"</h2>  </th>  <th> <h2> "+ AppWeight.getInstance().getString(R.string.reportweightcolumn)  +"   </h2>  </th> </tr>";
        if(cursor!=null && cursor.moveToFirst()) {
            while(!cursor.isAfterLast()){
                result = result + " <tr> " ;
                /**
                 * Date Row
                 */
                result = result + " <th> " ;
                result = result + cursor.getString(cursor.getColumnIndex(Constants.WEIGHT_TABLE_DATE_COLUMN));
                result = result + " </th> " ;
                /**
                 * Weight Row
                 */
                result = result + " <th> " ;
                result = result + cursor.getString(cursor.getColumnIndex(Constants.WEIGHT_TABLE_WEIGHT_COLUMN));
                result = result + " </th> " ;
                
                result = result + "</tr> ";
                cursor.moveToNext();
            }
        }
        result = result + "</table> </body> </html>";
        return result;
    }
	

	
}
